﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GBPVRLibrary.MetadataLibrary;
using System.Text.RegularExpressions;
using System.Reflection;

namespace GBPVRTestForm
{
    public partial class Form1 : Form
    {
        GBPVRLibrary.MetadataLibrary.MetadataLookup MetadataLookup { get; set; }
        private RegexOptions UtilsRegexOptions { get; set; }
        public Form1()
        {
            InitializeComponent();
            MetadataLookup = new GBPVRLibrary.MetadataLibrary.MetadataLookup();
            chkUtilsRegexOptions.Items.Add(RegexOptions.Compiled.ToString());
            chkUtilsRegexOptions.Items.Add(RegexOptions.CultureInvariant.ToString());
            chkUtilsRegexOptions.Items.Add(RegexOptions.ECMAScript.ToString());
            chkUtilsRegexOptions.Items.Add(RegexOptions.ExplicitCapture.ToString());
            chkUtilsRegexOptions.Items.Add(RegexOptions.IgnoreCase.ToString());
            chkUtilsRegexOptions.Items.Add(RegexOptions.IgnorePatternWhitespace.ToString());
            chkUtilsRegexOptions.Items.Add(RegexOptions.Multiline.ToString());
            chkUtilsRegexOptions.Items.Add(RegexOptions.RightToLeft.ToString());
            chkUtilsRegexOptions.Items.Add(RegexOptions.Singleline.ToString());
            chkUtilsRegexOptions.ItemCheck += new ItemCheckEventHandler(chkUtilsRegexOptions_ItemCheck);
        }

        void chkUtilsRegexOptions_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if(e.NewValue == CheckState.Checked)
                UtilsRegexOptions += (int)(RegexOptions)Enum.Parse(typeof(RegexOptions), (string)chkUtilsRegexOptions.Items[e.Index]);
            else if (e.NewValue == CheckState.Unchecked)
                UtilsRegexOptions -= (int)(RegexOptions)Enum.Parse(typeof(RegexOptions), (string)chkUtilsRegexOptions.Items[e.Index]);

            lblUtilsRegexOptionsValue.Text = String.Format("Value: {0}", (int)UtilsRegexOptions);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            List<string> types = MetadataLookup.Types;
            types.Insert(0, "");
            cboMetadataLookupMediaType.DataSource = types;
        }

        private void btnMetadataLookupSearch_Click(object sender, EventArgs e)
        {
            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
            worker.RunWorkerAsync(new object[] { cboMetadataLookupMediaType.SelectedItem.ToString(), cboMetadataLookupSource.SelectedItem.ToString(), txtMetadataLookupSearchText.Text, chkMetadataLookupIsSearch.Checked });
        }

        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            dgvMetadataLookupResults.AutoGenerateColumns = true;
            List<MetadataLookupSearchResult> results = e.Result as List<MetadataLookupSearchResult>;
            if (results != null)
            {
                dgvMetadataLookupResults.DataSource = results;
            }
            else if(e.Result != null)
            {
                Type type = e.Result.GetType();
                List<object> values = new List<object>();
                foreach (PropertyInfo pi in type.GetProperties())
                {
                    if ((from att in pi.GetCustomAttributes(false) select att.GetType()).Contains(typeof(MetadataAttribute)))
                        values.Add(new { Name= pi.Name, Value=pi.GetValue(e.Result, null) });                        
                }
                dgvMetadataLookupResults.DataSource = values;
            }
        }

        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            if((bool)((object[])e.Argument)[3])
                e.Result = MetadataLookup.SearchMediaTypeSource(((object[])e.Argument)[0].ToString(), ((object[])e.Argument)[1].ToString(), ((object[])e.Argument)[2].ToString());
            else
                e.Result = MetadataLookup.LoadItem(((object[])e.Argument)[0].ToString(), ((object[])e.Argument)[1].ToString(), ((object[])e.Argument)[2].ToString(), typeof(VideoMetadata) );
        }

        private void cboMetadataLookupMediaType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboMetadataLookupMediaType.SelectedItem == null || String.IsNullOrEmpty(cboMetadataLookupMediaType.SelectedItem.ToString()))
                return;
            List<string> sources = MetadataLookup.Sources(cboMetadataLookupMediaType.SelectedItem.ToString());
            sources.Insert(0, "");
            cboMetadataLookupSource.DataSource = sources;
        }

        private void btnVideoFileBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() != DialogResult.OK)
                return;
            txtVideoFileName.Text = ofd.FileName;
        }

        private void btnVideoFileCheck_Click(object sender, EventArgs e)
        {
            GBPVRLibrary.MetadataLibrary.VideoFileInformation info = new VideoFileInformation(txtVideoFileName.Text);

        }
    }
}
